package com.baboba.rummy.robot;

import java.util.LinkedList;
import java.util.List;

import com.baboba.rummy.card.Card;
import com.baboba.rummy.card.CardBitMap;
import com.baboba.rummy.card.Color;
import com.baboba.rummy.card.UserCard;

public class IceBreaker {
    
    public static List<LinkedList<UserCard>> awkwardBreak(CardBitMap bitMap){
            List<LinkedList<UserCard>> result = new LinkedList<LinkedList<UserCard>>();
            CardBitMap bitMapClone = bitMap;
            
            List<LinkedList<UserCard>> verticalCards = getAllVerticalAvailableCards(bitMapClone);
            result.addAll(verticalCards);
            
            List<LinkedList<UserCard>> horizontalCards = getAllHorizontalAvailableCards(bitMapClone);
            result.addAll(horizontalCards);
            
            finalProccessIceBreak(result);
            
            return result;
    }
    
    private static void finalProccessIceBreak(List<LinkedList<UserCard>> result) {
            //TODO 
    }

    public static List<LinkedList<UserCard>> getAllHorizontalAvailableCards(CardBitMap bitMap){
            List<LinkedList<UserCard>> result = new LinkedList<LinkedList<UserCard>>();
            Color[] colors = Color.values();
            
            for (Color color:colors){
                    LinkedList<UserCard> cards = new LinkedList<UserCard>();
                    for (int i=13;i>=1;i--){
                            if (bitMap.getCardBitMap(color.ordinal(), i)>0)
                                    cards.add(new UserCard(i, color));
                            else{
                                    if (cards.size()>=3){
                                            result.add(cards);
                                            cards = new LinkedList<UserCard>();
                                    }else
                                            cards.clear();
                            }
                    }
                    
                    cards.clear();
                    for (int i=13;i>=1;i--){
                            if (bitMap.getCardBitMap(color.ordinal(), i)>1)
                                    cards.add(new UserCard(i, color));
                            else{
                                    if (cards.size()>=3){
                                            result.add(cards);
                                            cards = new LinkedList<UserCard>();
                                    }else
                                            cards.clear();
                            }
                    }
            }
            
            return result;
    }
    
    public static List<LinkedList<UserCard>> getAllVerticalAvailableCards(CardBitMap bitMap){
            List<LinkedList<UserCard>> result = new LinkedList<LinkedList<UserCard>>();
            Color[] colors = Color.values();
            
            //from big to small
            for (int i=13;i>=1;i--){
                    //iterate every color
                    
                    LinkedList<UserCard> cards = new LinkedList<UserCard>();
                    for (Color color:colors){
                            if (bitMap.getCardBitMap(color.ordinal(), i)>0)
                                    cards.add(new UserCard(i, color));
                    }
                    
                    if (cards.size()>=3)
                            result.add(cards);
                    
                    cards = new LinkedList<UserCard>();
                    for (Color color:colors){
                            if (bitMap.getCardBitMap(color.ordinal(), i)>1)
                                    cards.add(new UserCard(i, color));
                    }
                    
                    if (cards.size()>=3)
                            result.add(cards);
            }
            
            return result;
    }
    
    private static List<LinkedList<UserCard>> checkHorizontal(CardBitMap bitMap){
            //removeCards();
            return null;
    }
    
    private static List<LinkedList<UserCard>> checkVertical(CardBitMap bitMap){
            /*Color[] colors = Color.values();
            
            //from big to small
            for (int i=13;i>=1;i--){
                    //iterate every color
                    for (Color color:colors){
                            if (bitMap.getCardBitMap(color.ordinal(), i)>0)
                                    //result++;
                    }
            }
            
            //removeCards();
            */
            return null;
    }
    
    private static boolean hasLeftSequence(CardBitMap bitMap, Color color, int number){
            if (number<3)
                    return false;
            
            return bitMap.getCardBitMap(color.ordinal(), number)>0
                            && bitMap.getCardBitMap(color.ordinal(), number-1)>0
                            && bitMap.getCardBitMap(color.ordinal(), number-2)>0;
    }
    
    private static int hasVerticalSame(CardBitMap bitMap, int number){
            Color[] colors = Color.values();
            int result = 0;
            
            for (Color color:colors){
                    if (bitMap.getCardBitMap(color.ordinal(), number)>0)
                            result++;
            }
            
            return result;
    }
    
    private static void removeCards(CardBitMap bitMap, List<UserCard> cards){
            for (Card card:cards){
                    if (card.getNumber()>0){
                            bitMap.decrease(card.getColor().ordinal() , card.getNumber());
                    }
            }
    }
}